<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" lock-scroll width="30%" height="30%">
      <div slot="title" class="header_title">
        <span class="title_span">选择类别</span>
        <div class="tree_input">
          <p>
            <input type="text" placeholder="搜索类别关键字" v-model="filterText" />
            <span>
              <i class="el-icon-search"></i>
            </span>
          </p>
        </div>
      </div>
      <div class="content_div">
        <div class="div_tree">
          <div class="tree_structure">
            <el-tree
              ref="tree"
              :data="treeData"
              node-key="label"
              :props="defaultProps"
              highlight-current
              default-expand-all
              :expand-on-click-node="false"
              @node-click="handleNodeClick"
              :filter-node-method="filterNode"
            >
              <span class="tree_node" slot-scope="{ node, data }">
                <span>{{ node.label }}</span>
                <span v-if="nowLabel.id == data.id" class="show_nodex">
                  <i class="el-icon-circle-plus-outline" @click.stop="onAddTree(data)"></i>
                  <i
                    v-if="data.parentId !== '0'"
                    class="el-icon-edit-outline"
                    @click.stop="onEditTree(node, data)"
                  ></i>
                  <i
                    v-if="data.parentId !== '0'"
                    class="el-icon-delete"
                    @click.stop="onDeleteTree(data)"
                  ></i>
                </span>
              </span>
            </el-tree>
          </div>
        </div>
        <div class="info_button">
          <el-button type="primary" @click="onSelectRow">选择</el-button>
          <el-button @click="onCancelRow">取消</el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 新增树形结构数据弹框 -->
    <el-dialog
      :title="this.newTreeData.module.name == 'deptName' ? '部门' : '类别信息'"
      :visible.sync="showNewTree"
      width="480px"
    >
      <NewCategory
        :newTreeData="newTreeData"
        @onSaveTreeChanges="onSaveTreeChanges"
        @onCancelTree="showNewTree = false"
      ></NewCategory>
    </el-dialog>
    <!-- 提醒弹框 -->
    <PopupReminder ref="reminderRef" :tipsData="tipsData" @onConfirm="onYes"></PopupReminder>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import NewCategory from '@/components/c_category/NewCategory'
import PopupReminder from './popupReminder'
import { getAction, deleteAction } from '@/api/manage'
export default {
  name: 'BasicInformation',
  computed: {
    ...mapGetters(['name']),
  },
  components: {
    NewCategory,
    PopupReminder,
  },
  props: {
    toTreeData: Object,
  },
  created () {
    // this.getTreeData()
  },
  data () {
    return {
      filterText: '',
      nowLabel: {},
      treeData: [],
      defaultProps: {
        children: 'children',
        label: this.toTreeData.module.name,
      },
      showNewTree: false, //显示树形结构数据新增弹框
      // 传递数据给新增树形结构组件
      newTreeData: {
        parentData: '',
        data: '',
        state: '',
        module: {
          name: ''
        }
      },
      // 传递给弹框的数据
      tipsData: {
        tipsState: '',
        tipsWords: '',
        tipsMark: '',
      },
      nowTreeId: '',
      dialogVisible: false, // 新增弹框
    }
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    filterNode (value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 获取类别数据
    getTreeData () {
      getAction(this.toTreeData.getTreeUrl, {}).then((res) => {
        if (res.code === 200) {
          // this.treeData = res.result
          let list = res.result
          this.treeData = this.getTreeDataByList(list, '0', '')
        }
      })
    },
    getTreeDataByList (list, parentId, parentName) {
      let data = []
      for (let index = 0; index < list.length; index++) {
        const element = list[index];
        let pid = element[this.toTreeData.module.pid]
        if (pid == parentId) {
          let label = element[this.toTreeData.module.name]
          data.push({
            ...element,
            id: element.id,
            label,
            parentId,
            parentName,
            children: this.getTreeDataByList(list, element.id, label)
          })
        }
      }
      return data
    },
    // 提醒弹框确认后操作
    onYes (mark) {
      if (mark == 'onDeleteTree') {
        let data = {
          id: this.nowTreeId,
        }
        deleteAction(this.toTreeData.deleteTreeUrl, data).then((res) => {
          if (res.code === 200) {
            this.getTreeData()
          } else {
            this.$elMessage({
              showClose: true,
              message: res.message,
              type: 'error',
              iconClass: 'el-icon-warning-outline',
              duration: 3000,
            })
          }
        })
      }
    },
    // 搜索树形结构数据
    onSelectTree () {
      this.getTreeData()
    },
    // 触发树形当前节点
    handleNodeClick (data) {
      this.nowLabel = data
    },
    // 触发新增树形节点按钮
    onAddTree (data) {
      this.showNewTree = true
      this.newTreeData = {
        parentData: data,
        state: 'add',
        ...this.toTreeData,
      }
    },
    // 触发修改树形节点按钮
    onEditTree (node, data) {
      this.showNewTree = true
      this.newTreeData = {
        parentData: node.parent.data,
        data: data,
        state: 'edit',
        ...this.toTreeData,
      }
    },
    // 类别信息新增/修改后数据获取
    onSaveTreeChanges () {
      this.showNewTree = false
      this.getTreeData()
    },
    // 类别信息取消按钮触发
    onCancelTree () {
      this.showNewTree = false
    },
    // 触发删除树形节点按钮
    onDeleteTree (data) {
      if (data.children.length) {
        this.$elMessage({
          showClose: true,
          message: '类别下级有分类，不允许删除！',
          type: 'error',
          iconClass: 'el-icon-warning-outline',
          duration: 3000,
        })
      } else {
        this.tipsData.tipsState = 'delete'
        this.tipsData.tipsWords = '是否确认删除?'
        this.tipsData.tipsMark = 'onDeleteTree'
        this.$refs.reminderRef.dialogVisible = true
        this.nowTreeId = data.id
      }
    },
    // 选中后传递给父组件
    onSelectRow () {
      var objStr = JSON.stringify(this.nowLabel)
      if (objStr == '{}') {
        this.$elMessage.warning('请选择一条信息')
      } else {
        this.$emit('onSelectTree', this.nowLabel)
        this.dialogVisible = false
        this.nowLabel = {}
      }
    },
    onCancelRow () {
      this.$emit('onCancelTree', this.nowLabel)
      this.dialogVisible = false
      this.nowLabel = {}
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  width: 100%;
  margin-top: 24vh !important;
  .el-dialog__body {
    padding: 0;
  }
  .el-dialog__header {
    width: 100%;
    height: 0.937559rem;
    box-sizing: border-box;
    padding: 0 0.187512rem 0 0.281268rem;
    border-bottom: 1px solid #eeeeee;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .el-dialog__headerbtn {
      position: static;
      .el-icon-close {
        color: #999;
        font-size: 0.48753rem;
      }
    }
  }
  .header_title {
    width: 100%;
    display: flex;
    align-items: center;

    .tree_input {
      width: 3.750234rem;
      height: 32px;
      box-sizing: border-box;
      margin-left: 0.468779rem;
      p {
        width: 100%;
        height: 32px;
        margin: 0;
        box-sizing: border-box;
        border: 1px solid #dddddd;
        display: flex;
        align-items: center;
        position: relative;
        input {
          width: 100%;
          box-sizing: border-box;
          height: 30px;
          border: none;
          font-size: 13px;
          &:focus {
            outline: none;
          }
          &::placeholder {
            color: #d3d3d3;
          }
        }
        span {
          position: absolute;
          right: 0;
          display: flex;
          align-items: center;
          justify-content: center;
          width: 0.562535rem;
          height: 28px;
          background: #fff;
          .el-icon-search {
            font-size: 0.31877rem;
            color: rgb(211, 211, 211);
          }
        }
      }
    }
  }
}
.content_div {
  width: 100%;

  background: #fff;
  box-sizing: border-box;
  .tree_structure {
    height: 40vh;
    margin: 20px;
    box-sizing: border-box;
    border: 0.018751rem solid #e3e3e3;
    overflow: auto;
    //滚动条的宽度
    ::-webkit-scrollbar {
      width: 10px;
      height: 10px;
      background-color: #e5e5e5;
    }
    //滚动条的滑块
    ::-webkit-scrollbar-thumb {
      background-color: #c9c9c9;
    }
    ::v-deep .el-tree {
      width: 100%;
      .tree_node {
        width: 100%;
        display: flex;
        align-items: center;
        span {
          font-size: 0.225014rem;
        }
        .show_nodex {
          margin-left: 0.562535rem;
          font-size: 0.300019rem;
          i {
            margin-left: 0.150009rem;
            font-weight: 600;
          }
          .el-icon-circle-plus-outline {
            color: #1993fb;
          }
          .el-icon-edit-outline {
            color: #1993fb;
          }
          .el-icon-delete {
            color: #ff2626;
          }
        }
      }

      .el-tree-node__children {
        overflow-x: auto;
        .el-tree-node__label {
          color: #666;
          font-weight: normal;
        }
      }
      .el-tree-node__label {
        font-size: 0.225014rem;
        font-weight: 600;
      }
      .is-current {
        .el-tree-node__content {
          background-color: #e6f7ff;
        }
      }
      .el-tree-node__content > .el-tree-node__expand-icon {
        padding-left: 0;
      }
    }
  }

  .info_button {
    width: 100%;
    height: 1.312582rem;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    ::v-deep .el-button {
      width: 1.12507rem;
      height: 0.562535rem;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 0.093756rem;
    }
  }
}
</style>